package com.dev;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import com.fb.FileBrowserPage;
import com.dev.MainActivity;
public class ShowFileClipBoard extends LinearLayout implements OnClickListener{
	WindowManager.LayoutParams wlp=new WindowManager.LayoutParams();
	LinearLayout titlebar;
	TextView title;
	IconViewClose ivc;
	IconView purge;
	ListView clips;
	ShowClips sc=new ShowClips();
	FileBrowserPage fbp;
	MainActivity ma;
	public ShowFileClipBoard(MainActivity ma){
		super(ma);
		this.ma=ma;
		setOrientation(VERTICAL);
		setWillNotDraw(false);
		title=new TextView(ma);
		title.setTextSize(TypedValue.COMPLEX_UNIT_DIP,13);
		title.setGravity(Gravity.CENTER_VERTICAL);
		title.setTextColor(0xff7f7f7f);
		title.setText("剪贴板");
		title.setPadding(9,0,0,0);
		ivc=new IconViewClose(ma);
		ivc.setOnClickListener(this);
		purge=new IconView(ma);
		purge.setImageResource(R.drawable.ic_sweep);
		purge.setOnClickListener(this);
		titlebar=new LinearLayout(ma){
			@Override
			public void onDraw(Canvas c){
				super.onDraw(c);
				Paint p=new Paint();
				p.setColor(FileBrowserPage.borderColor);
				c.drawRect(0,getHeight()-FileBrowserPage.borderWidth,getWidth(),getHeight(),p);
			}
		};
		titlebar.setWillNotDraw(false);
		titlebar.setOrientation(HORIZONTAL);
		titlebar.addView(title,new LayoutParams(0,-1,1));
		titlebar.addView(purge);
		titlebar.addView(ivc);
		clips=new ListView(ma);
		clips.setAdapter(sc);
		addView(titlebar,-1,ma.dialogTitleBarHeight);
		addView(clips,-1,-2);
	}
	public void show(FileBrowserPage fbp){
		this.fbp=fbp;
		wlp.width=ma.gui.getWidth()*4/5;
		wlp.height=-2;
		ma.wm.addView(this,wlp);
	}
	void dismiss(){
		ma.wm.removeView(this);
	}
	@Override
	public void onClick(View v){
		if(v==ivc){
			dismiss();
		}else if(v==purge){
			dismiss();
			ma.fileClipBoard.clear();
			fbp.ivPaste.setVisibility(View.GONE);
		}
	}
	@Override
	protected void onDraw(Canvas canvas){
		super.onDraw(canvas);
		Paint p=new Paint();
		p.setColor(FileBrowserPage.borderColor);
		canvas.drawColor(0xffffffff);
		canvas.drawRect(0,0,FileBrowserPage.borderWidth,getHeight(),p);
		canvas.drawRect(0,0,getWidth(),FileBrowserPage.borderWidth,p);
		canvas.drawRect(getWidth()-FileBrowserPage.borderWidth,0,getWidth(),getHeight(),p);
		canvas.drawRect(0,getHeight()-FileBrowserPage.borderWidth,getWidth(),getHeight(),p);
	}
	class ShowClips extends BaseAdapter{
		@Override
		public Object getItem(int position){
			return null;
		}
		@Override
		public long getItemId(int position){
			return 0;
		}
		@Override
		public int getCount(){
			return ma.fileClipBoard.size();
		}
		@Override
		public View getView(int position,View convertView,ViewGroup parent){
			if(convertView instanceof ClipItem){
				ClipItem ci=(ClipItem)convertView;
				ci.setIndex(position);
				return ci;
			}else{
				ClipItem ci=new ClipItem(position);
				ci.setIndex(position);
				return ci;
			}
		}
		class ClipItem extends LinearLayout implements OnClickListener,OnLongClickListener{
			int index;
			IconView remove;
			TextView action;
			TextView name;
			ClipItem(int p){
				super(ma);
				this.index=p;
				setOrientation(HORIZONTAL);
				remove=new IconView(ma);
				remove.setImageResource(R.drawable.ic_trash_can);
				remove.setOnClickListener(this);
				action=new TextView(ma);
				action.setTextColor(0x77888888);
				action.setGravity(Gravity.CENTER);
				action.setTextSize(TypedValue.COMPLEX_UNIT_DIP,12);
				name=new TextView(ma);
				name.setTextColor(0xff000000);
				name.setTextSize(TypedValue.COMPLEX_UNIT_DIP,12);
				name.setGravity(Gravity.CENTER_VERTICAL);
				name.setPadding(9,0,0,0);
				name.setSingleLine();
				name.setBackground(new FileBrowserPage.Pressed());
				name.setEllipsize(TextUtils.TruncateAt.START);
				name.setOnClickListener(this);
				addView(name,new LinearLayout.LayoutParams(0,-1,1));
				addView(action,(int)(40*ma.dp),-1);
				addView(remove);
				setIndex(p);
				ListView.LayoutParams llp=new ListView.LayoutParams(-1,(int)(25*ma.dp));
				setLayoutParams(llp);
			}
			void setIndex(int i){
				this.index=i;
				FileClipData fcd=ma.fileClipBoard.get(i);
				name.setText(fcd.f.getAbsolutePath());
				if(fcd.move){
					action.setText("剪切");
				}else{
					action.setText("复制");
				}
			}
			@Override
			public void onClick(View v){
				if(v==name){
					if(ma.fileDetails==null)ma.fileDetails=new ShowFileDetails(ma);
					ma.fileDetails.show(ma.fileClipBoard.get(index).f,fbp);
				}else if(v==remove){
					if(ma.fileClipBoard.size()>1){
						ma.fileClipBoard.remove(index);
						sc.notifyDataSetChanged();
					}else{
						ma.fileClipBoard.clear();
						dismiss();
						fbp.ivPaste.setVisibility(View.GONE);
					}
				}
			}
			@Override
			public boolean onLongClick(View v){
				return true;
			}
		}
	}
}
